budgetThird = 1;
const treeFun = (function() {
	let rateparams = {
		username: getUsername,
		budgetYear: localStorage.getItem("year"),
		budgetMonth: localStorage.getItem("month"),
		dept: '',
		status: 2,
		chartPage: 70404,
		chartNo: 5
	}
	let Request = new Object();
	Request = GetRequest();
	const index = Request['index'];
	const type = decodeURI(Request['type'])
	$.get(
		`${API_URL}getdata`, {
			...rateparams,
		},
		function(res) {
			let re = eval("(" + res + ")");
			console.log(re)
			let data = re.items[0].charts[0].data;
			let pthem = '';
			for (const item of data) {
				if (item.key01 === localStorage.getItem('budgetPPID')) {
					pthem = item.key03;
				}
				if (item.key01 === localStorage.getItem('budgetPID')) {
					document.querySelector('.vessel .aside').innerHTML = `
										<div class="title">
											<span class="third">预算执行/</span>
											<span class="fourth">${pthem}/</span>
											<span class="fifth">${item.key03}/</span>
											<h4>${type}</h4>
										</div>`
				}
			}
			// localStorage.setItem("budgetID", document.querySelectorAll('.vessel .main ul li')[0].dataset.id)
			// localStorage.setItem("budgetPPID", data[0].key01)
			
			$.get(
				`${API_URL}${localStorage.getItem("treeTotal")}`, {},
				function(res) {
					const re = eval("(" + res + ")");
					const data = re.items[0].charts[0].data[0];
					console.log(data)
					document.querySelector('.vessel .aside').innerHTML += `
										<div class="statistics">
											<h4>执行总额</h4>
											<h3>${data.key01}<span>万元</span></h3>
										</div>`
										
										
					document.querySelector('.third').onclick = function() {
						window.location.href = 'budget-enforcement-third.html';
					}
					
					document.querySelector('.fourth').onclick = function() {
						const obj = eval('(' + localStorage.getItem('saveID') + ')')
						localStorage.setItem("budgetPPID", obj.ppid)
						localStorage.setItem("budgetPID", obj.pid)
						window.location.href = 'budget-enforcement-fourth.html';
					}
					document.querySelector('.fifth').onclick = function() {
						const obj = eval('(' + localStorage.getItem('saveID') + ')')
						localStorage.setItem("budgetPPID", obj.pid)
						localStorage.setItem("budgetPID", obj.iid)
						const type = $(this).text().slice(0,$(this).text().length-1)
						window.location.href = `budget-enforcement-fifth-cl.html?type=${type}`;
					}
				}
			)
			
		}

	)
	
	

	// getratestate()

	// function getratestate() {
	// 	$.get(
	// 		`${API_URL}getdata`, {
	// 			...rateparams,
	// 			chartPage: 70402,
	// 			budgetNo: localStorage.getItem("budgetID"),
	// 			chartNo: 3
	// 		},
	// 		function(res) {
	// 			let re = eval("(" + res + ")");
	// 			if(re.items[0].charts[0].data.length>0){
	// 					document.querySelector('#rateMask .mask-scroll .state .p').innerHTML = re.items[0]
	// 						.charts[0].data[0].key01
	// 				}
				
	// 		})
	// }


	//月度到款执行
	function getMonPayComplete(dataurl) {
		$.get(
			`${API_URL}${dataurl}`, {},
			function(res) {
				const re = eval("(" + res + ")");
				const data = re.items[0].charts[0].data;
				console.log(data)
				if(data.length === 0){
					if(document.querySelector('#monthlj')){
						document.querySelector('#monthlj')&&document.querySelector('#monthlj').remove()
						document.querySelector('.month').innerHTML += `<div class="nowrap">
										<div class="noData">
											<img src="assets/images/mask/budget-All-IncomingPayment.svg" >
											<p>暂无数据</p>
										</div>
									</div>`
					}
					return
				}
				if(document.querySelector('.nowrap')){
					document.querySelector('.nowrap').remove();
					document.querySelector('.month').innerHTML += `<div id="monthlj"></div>`
				}
				let xData = [];
				let currYearBudget = [];
				const legend = re.items[0].charts[0].chart_name.split(' ').splice(2);
				const legendData = legend.map(item => {
					let newIt = item.split('-').splice(1)[0]
					if (newIt.indexOf('{') > -1) {
						newIt = newIt.replace('{', '')
					}
					if (newIt.indexOf('}') > -1) {
						newIt = newIt.replace('}', '')
					}
					return newIt
				})

				for (var i = 0; i < data.length; i++) {
					xData.push(+data[i].key01);
					currYearBudget.push(data[i].key02);
				}

				let Data = [
					{
						name: "执行金额",
						type: "bar",
						barWidth: '30%',
						barGap: 0,
						itemStyle: {
							color: "#50CD89",
						},
						data: currYearBudget,
					},
				];
				have("monthlj") ? mulitYBBar("monthlj", xData, legendData, Data,'月',true,false) : "";
			})

	}


	// document.querySelector('#rateMask .mask-scroll .state div .edit').onclick = function() {
	// 	document.querySelector(
	// 			'#editMask .mask-content textarea').value = document
	// 		.querySelector('#rateMask .mask-scroll .state .p').innerText
	// 	toggle()
	// }


	// document.querySelector('#editMask .mask-content .btn button.cancle').onclick = function() {
	// 	toggle()
	// }

	// function toggle() {
	// 	document.querySelector('#editMask').style.display = document.querySelector(
	// 		'#editMask').style.display !== 'block' ? 'block' : 'none'
	// }


	// document.querySelector('#editMask .mask-content .btn button.save').onclick = () => {
	// 	$.get(
	// 		`${API_URL}getdata`, {
	// 			...rateparams,
	// 			chartNo: 4,
	// 			chartPage: 70402,
	// 			budgetNo: localStorage.getItem("budgetID"),
	// 			key01: document.querySelector(
	// 				'#editMask .mask-content textarea').value
	// 		},
	// 		function(res) {
	// 			let re = eval("(" + res + ")");
	// 			alert(re.items[0].charts[0].chart_name)
	// 			getratestate()
	// 			toggle()
	// 		})
	// }


	getTree()
	function GetRequest() {
	    var url = encodeURI(window.location.search); //获取url中"?"符后的字串
	    var theRequest = new Object();
	    if (url.indexOf("?") != -1) {
	        var str = url.substr(1);
	        strs = str.split("&");
	        for(var i = 0; i < strs.length; i ++) {
	            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
	        }
	    }
	    return theRequest;
	}  
	function getTree() {
		$.get(
			`${API_URL}${localStorage.getItem("treeCourse")}`, {},
			function(res) {
				let re = eval("(" + res + ")");
				const data = re.items[0].charts[0].data;
				const _data = data.map(item => item['key02']).filter((item,index) => {
					return data.map(item => item['key02']).indexOf(item) === index
				})
				const allData = [{
					name:type,
					value:0,
					children:[]
				}]
				for(let i=0;i<_data.length;i++){
					allData[0].children.push({
						name:_data[i],
						children:[]
					})
					for(let j=0;j<data.length;j++){
						if(data[j]['key02'] === _data[i]){
							allData[0].children[i].children.push({
								name:data[j].key01,
								children:[{
									name:'执行金额',
									num: data[j].key03?data[j].key03:'0.00',
									pname:data[j].key01,
									course:data[j].key04
								}]
							})
						}
					}
				}
				// have("tree-chart") ? dimensionTree1("tree-chart", allData,colors) : "";
				have("tree-chart") ? dimensionRelation("tree-chart", allData) : "";
		})

		
	}

	document.querySelector('.mask-wrap .mask-title img').onclick = function() {
		document.querySelector('#rateMask').style.display = 'none'
	}
	return {
		getMonPayComplete,
		// getratestate
	}
})()

function treeNodeclick(param) {
	if (param.data.num) {
		treeFun.getMonPayComplete(param.data.course)
		document.querySelector('#rateMask').style.display = 'block'
	}
}
